package com.woniu.smallmedical.controller;

import com.woniu.smallmedical.entity.Department;
import com.woniu.smallmedical.entity.PageBean;
import com.woniu.smallmedical.entity.ResponseResult;
import com.woniu.smallmedical.server.DepartmentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DepartmentController {
    @Autowired
    DepartmentServer departmentServer;

    @GetMapping("/getAllDepartment")
    public ResponseResult getAllDepartment(){
        List<Department> allDepartment = departmentServer.getAllDepartment();
        return new ResponseResult(allDepartment,200,"科室！");
    }

//    获取所有科室并分页
    @RequestMapping("/departmentList")
    public ResponseResult<PageBean<Department>> departmentList(@RequestBody PageBean pageBean){
        // 没有查询条件
    if(pageBean.getPageUrl()=="" || pageBean.getPageUrl()==null) {
        pageBean.setPageBeanList(departmentServer.departmentList(pageBean));
    }//有查询条件
    if(pageBean.getPageUrl()!=null || pageBean.getPageUrl()!=""){
        pageBean.setPageBeanList(departmentServer.departmentListByUrl(pageBean));
        }
        pageBean.setTotalRecord(departmentServer.getCount());
        return  new ResponseResult(pageBean,200,"ok");
    }
//    修改科室状态（1：启用，2：停用）
    @RequestMapping("/changeswitch")
    public ResponseResult changeSwitch(@RequestBody Department department){
        System.out.println(department);
        department.setDstatus(department.getDstatus()==1?2:1);
        System.out.println(department);
          int dstatus = departmentServer.changeswitch(department);
        return new ResponseResult(department.getDstatus(),200,"修改状态成功");
    }
//    增加科室
    @RequestMapping("/addDepartment")
    public ResponseResult addDepartment(@RequestBody Department department){
        System.out.println(department);
    try {
        if(department.getDstatus()==0 || department.getDate()==null ||department.getDdes()==null || department.getDname()==""){
            return  new ResponseResult(null,400,"信息不能为空！");
        }else{
            int count = departmentServer.addDepartment(department);
            if(count==1) {
                return new ResponseResult(count, 200, "添加科室'" + department.getDname() + "'成功");
            }else {
                return new ResponseResult(count, 400, "添加科室'" + department.getDname() + "'失败,请检查信息是否完整");
            }
        }
    }catch (NullPointerException n){
        return  new ResponseResult(null,400,"请填入完整信息！");
    }

    }
//    修改科室信息
    @RequestMapping("/changedepartmentListInfo")
    public ResponseResult changedepartmentListInfo(@RequestBody Department department){
        System.out.println(department);
        try {
            if(department.getDstatus()==0 || department.getDate()==null ||department.getDdes()==null || department.getDname()==""){
                return  new ResponseResult(null,400,"信息不能为空！");
            }else{
                int count = departmentServer.changedepartmentListInfo(department);
                if(count==1) {
                    return new ResponseResult(count, 200, "修改科室'" + department.getDname() + "'成功");
                }else {
                    return new ResponseResult(count, 400, "修改科室'" + department.getDname() + "'失败,请检查信息是否完整");
                }
            }
        }catch (NullPointerException n){
            return  new ResponseResult(null,400,"请填入完整信息！");
        }
    }
//    删除科室（状态值改为3）
    @RequestMapping("/deleteDepartment")
    public ResponseResult deleteDepartment(@RequestBody Department department){
    int count = departmentServer.deleteDepartment(department);
    if(count==1) {
        return new ResponseResult(count, 200, "删除成功");
    }else {
        return new ResponseResult(count, 500, "删除失败");
    }
    }
}